$(document).ready(function () {
    showTime();
    /*查询科目*/
    $.ajax({
        type: "POST",
        url: "fields",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#field").html($("#field").html() + '<option value="' + data[i].fieldId + '">' + data[i].fieldName + '</option>')
            }
        }
    });
    selected();

});

//固定倒计时显示位置
window.onscroll = function () {
    var oDiv = document.getElementById("s");
    oDiv.style.top = document.body.scrollTop + 120;  //控制上下位置
    oDiv.style.left = document.body.scrollLeft + 1000; //控制横向位置

}
//设定倒数秒数
var t = 6060;
//显示倒数秒数
function showTime() {
    t -= 1;
    $("#times").val(parseInt(t / 60));
    if (t == 600) {
        layer.alert("距离交卷只剩10分钟");
    }
    if (t == 0) {
        submit();
    }
    //每秒执行一次,showTime()
    setTimeout("showTime()", 1000);
}

function selected() {
    $("#a").show();
    $("#b").show();
    $("#c").show();
    $("#d").show();
    $("#questionPaper").show();
    $("#questionPaper2").show();
    $("#questionPaper3").show();
    $("#questionPaper4").show();
    var fieldid = $("#field").val();
    if (fieldid == null) {
        fieldid = 1;
    }
    //加载选择题
    xzt(fieldid);
    //加载判断题
    pdt(fieldid);
    //加载填空题
    tkt(fieldid);
    //加载主观题
    zgt(fieldid);
}

//试题长度45自动换行
function CutStr(strx, lenx) {
    var sx = "";
    if (lenx > 45) {
        var lenx2 = Math.ceil(lenx / 45);
        for (var i = 1; i <= lenx2; i++) {
            sx += strx.substring((i - 1) * 45, i * 45) + "<br>";
        }
        return sx;
    }
    else {
        sx = strx + "<br>";
        return sx;
    }
}


var num;
var num2;
var num3;
var num4;

function xzt(fieldid) {
    $.ajax({
        type: "POST",
        dataType: "json",
        data: {"fieldid": fieldid},
        url: "studentExam",
        success: function (data) {
            num = data.length;
            var str = "";
            if (data.length > 0) {
                jQuery.each(data, function (i, item) {
                    var index = i + 1;
                    var options = new Array();
                    options = item.answer.answerOption.split(";");
                    str += "<form>" + index + "、" + CutStr(item.questionName, item.questionName.length);
                    str += "<input id='tips" + i + "' type='hidden' value=" + item.questionId + ">";
                    str += "<input name='question" + i + "'type=\"radio\" value=" + options[0] + "/>" + options[0] + "<br>";
                    str += "<input name='question" + i + "'type=\"radio\" value=" + options[1] + "/>" + options[1] + "<br>";
                    str += "<input name='question" + i + "'type=\"radio\" value=" + options[2] + "/>" + options[2] + "<br>";
                    str += "<input name='question" + i + "'type=\"radio\" value=" + options[3] + "/>" + options[3] + "<br>";
                    str += "</form>";
                    $("#questionPaper").html(str);
                });
            }
            else {
                $("#a").hide();
                $("#questionPaper").hide();
            }
        },
        error: function () {
            layer.alert("数据读取错误2!");
        }
    });
}

function pdt(fieldid) {
    $.ajax({
        type: "POST",
        dataType: "json",
        data: {"fieldid": fieldid},
        url: "studentExam2",
        success: function (data) {
            num2 = data.length;
            var str = "";
            if (data.length > 0) {
                jQuery.each(data, function (i, item) {
                    var index = i + 1;
                    var options = new Array();
                    options = item.answer.answerOption.split(";");
                    str += "<form>" + index + "、" + CutStr(item.questionName, item.questionName.length);
                    str += "<input id='tips2" + i + "' type='hidden' value=" + item.questionId + ">";
                    str += "<input name='question2" + i + "'type=\"radio\" value=" + options[0] + "/>" + options[0] + "<br>";
                    str += "<input name='question2" + i + "'type=\"radio\" value=" + options[1] + "/>" + options[1] + "<br>";
                    str += "</form>";
                    $("#questionPaper2").html(str);
                });
            }
            else {
                $("#b").hide();
                $("#questionPaper2").hide();
            }
        },
        error: function () {
            layer.alert("数据读取错误2!");
        }
    });
}

function tkt(fieldid) {
    $.ajax({
        type: "POST",
        dataType: "json",
        data: {"fieldid": fieldid},
        url: "studentExam3",
        success: function (data) {
            num3 = data.length;
            var str = "";
            if (data.length > 0) {
                jQuery.each(data, function (i, item) {
                    var index = i + 1;
                    str += "<input id='tips3" + i + "' type='hidden' value=" + item.questionId + ">";
                    str += index + "、" + CutStr(item.questionName, item.questionName.length);
                    str += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id='tktda" + i + "' type='text' style='width: 180px;height: 35px;'><br><br>";
                    $("#questionPaper3").html(str);
                });
            }
            else {
                $("#c").hide();
                $("#questionPaper3").hide();
            }
        },
        error: function () {
            layer.alert("数据读取错误2!");
        }
    });
}

function zgt(fieldid) {
    $.ajax({
        type: "POST",
        dataType: "json",
        data: {"fieldid": fieldid},
        url: "studentExam4",
        success: function (data) {
            num4 = data.length;
            var str = "";
            if (data.length > 0) {
                jQuery.each(data, function (i, item) {
                    var index = i + 1;
                    str += index + "、" + item.questionName + "<br>";
                    str += "<input id='tips4" + i + "' type='hidden' value=" + item.questionId + ">";
                    str += "<textarea id='tips5" + i + "' type='text' style='width: 540px;height: 130px;'></textarea><br><br>";
                    $("#questionPaper4").html(str);
                });
            }
            else {
                $("#d").hide();
                $("#questionPaper4").hide();
            }
        },
        error: function () {
            layer.alert("数据读取错误2!");
        }
    });
}

var qAnswer = [];
var qId = [];
var zgtId = [];
function submit() {
    isnull();
    isnull2();
    qAnswer.length = 0;
    qId.length = 0;
    zgtId.length = 0;
    //获取选择题id和答案
    for (var i = 0; i < num; i++) {
        var tip1 = "tips" + i;
        var questionid = $("#" + tip1).val();   //问题id
        var tip2 = "question" + i;   //单选按钮name
        var tip3 = $("input[name=" + tip2 + "]:checked").val();  //选中的按钮值
        qId.push(questionid);
        qAnswer.push(tip3);
    }
    //获取判断题id和答案
    for (var j = 0; j < num2; j++) {
        var tip4 = "tips2" + j;
        var questionid2 = $("#" + tip4).val();   //问题id
        var tip5 = "question2" + j;   //单选按钮name
        var tip6 = $("input[name=" + tip5 + "]:checked").val();  //选中的按钮值
        qId.push(questionid2);
        qAnswer.push(tip6);
    }
    //获取填空题id和答案
    for (var k = 0; k < num3; k++) {
        var tip7 = "tips3" + k;
        var questionid3 = $("#" + tip7).val();   //问题id
        var tip8 = "tktda" + k;   //单选按钮name
        var tip9 = $("#" + tip8).val();  //选中的按钮值
        qId.push(questionid3);
        qAnswer.push(tip9);
    }
    for (var m = 0; m < num4; m++) {
        var tip10 = "tips4" + m;
        var questionid4 = $("#" + tip10).val();
        var tip11 = "tips5" + m;
        var zgtA = $("#" + tip11).val() + "&";
        zgtId.push((questionid4));
        zgtId.push((zgtA));
    }
    //   if (isnull() && isnull2()) {
    $.ajax({
        type: "POST",
        dataType: "json",
        data: {"qId": qId, "qAnswer": qAnswer, "field": $("#field").val(), "zgtId": zgtId},
        url: "submitPaper",
        success: function (data) {
            if (data == 1) {
                layer.alert("提交成功！");
                location.href = "gradeStatisticView";
            }
            else {
                layer.alert("其他原因，提交失败1！");
            }
        },
        error: function () {
            layer.alert("其他原因，提交失败2！");
        }
    });
    //   }
}

//输入框文本域判空
function isnull() {
    var inputObj = document.getElementsByTagName("input");
    for (var i = 0; i < inputObj.length; i++) {
        if (inputObj[i].type == "" || inputObj[i].type == "text") {
            if (inputObj[i].value == "" || inputObj[i].value == null) {
                var objid = document.getElementById(inputObj[i].id);
                $(objid).val("考生没有填写该题");
                continue;
            }
        }
    }
}

function isnull2() {
    var textObj = document.getElementsByTagName("textarea");
    for (var i = 0; i < textObj.length; i++) {
        if (textObj[i].type == "" || textObj[i].type == "textarea") {
            if (textObj[i].value == "" || textObj[i].value == null) {
                var objid2 = document.getElementById(textObj[i].id);
                $(objid2).val("考生没有填写该题");
                continue;
            }
        }
    }
}
